// pages/article/index.js
const app = getApp()
var t = getApp(), a = t.requirejs("core"), e = t.requirejs("wxParse/wxParse"), i = t.requirejs("biz/diypage"), s = t.requirejs("biz/diyform"), n = t.requirejs("biz/goodspicker"), o = (t.requirejs("foxui"), 
t.requirejs("jquery"));
Page({
  data: {
    id:'',//文章id
    pageIndex:1,
    totalPage:1,
    dataList:[],
    info:{}
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id:options.id
      })
    }
  },
  goBoard(){
    wx.navigateTo({
      url: '/pages/community/essence/index?bid='+this.data.info.board.id,
    })
  },
  //投诉文章
  goComplaintArticle(){
    wx.navigateTo({
      url: '/pages/community/article/complaint/index?id='+this.data.id+'&mode=1&author='+this.data.info.post.nickname,
    })
  },
  commentPlaint(e){
    let id=e.currentTarget.dataset.id;
    let index=e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/community/article/complaint/index?id='+id+'&mode=2&author='+this.data.dataList[index].nickname,
    })
  },
  //获取文章详情
  geAticle(){
    let that=this;
    a.post("sns/board/get_board_detail", {
      id:that.data.id,
    }, function(res) {
      res.post.createtime=that.formatDate(new Date(res.post.createtime*1000))
      
      that.setData({
        info:res
      });
      that.getcommnt();
    })
  },
  getcommnt(){
    var that = this;
    a.post("sns/board/get_board_list", {
      bid:that.data.info.board.id,
      pid:that.data.id,
      page:that.data.pageIndex
    }, function(res) {
      let total = res.total;
      let size = res.pagesize;
      let integer = parseInt(total / size)
      let remainder = (total % size);
      let totalPage = integer;
      if (remainder > 0) {
        totalPage++
      }
      let data=[];
      if(that.data.pageIndex==1){
        data=res.list
      }else{
        data=that.data.dataList.concat(res.list)
      }
      that.setData({
        dataList:data,
        totalPage:totalPage,
      })
    })
  },
  //删除文章
  delArticle(){
    let that=this;
    wx.showModal({
      content: '是否删除该文章?',
      success:res=>{
        if(res.confirm){
          a.post("sns/board/delete", {
            bid:that.data.info.board.id,
            pid:that.data.id
          }, function(res) {
            if(res.error==-2){
              wx.showToast({
                title:res.message,
                icon:'none'
              })
              return
            }
            wx.setStorageSync('updateArticle', true)
            wx.navigateBack();
          })
        }
      }
    })
  },
  del(e){
    let index=e.currentTarget.dataset.index;
    let that=this;
    wx.showModal({
      content: '是否删除该评论?',
      success:res=>{
        if(res.confirm){
          a.post("sns/board/delete", {
            bid:that.data.dataList[index].bid,
            pid:that.data.dataList[index].id
          }, function(res) {
            if(res.error==-2){
              wx.showToast({
                title:res.message,
                icon:'none'
              })
              return
            }
            let dataList=that.data.dataList;
            dataList.splice(index,1);
            that.setData({
              dataList:dataList
            });
            if(that.data.pageIndex<that.data.totalPage){//获取下面一条数据
              that.getNextComment()
            }
          })
          
        }
      }
    })
  },
  getNextComment(){
    var that = this;
    a.post("sns/board/get_board_list", {
      bid:that.data.info.board.id,
      pid:that.data.id,
      page:that.data.pageIndex
    }, function(res) {
      let total = res.total;
      let size = res.pagesize;
      let integer = parseInt(total / size)
      let remainder = (total % size);
      let totalPage = integer;
      if (remainder > 0) {
        totalPage++
      }
      if(res.list.length>0){
        let data=res.list[res.list.length-1];
        that.setData({
          dataList:that.data.dataList.concat(data),
          totalPage:totalPage,
        })
      }
      
    })
  },
  formatDate(now) {
      var year = now.getFullYear();  //取得4位数的年份
      var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
      var date = now.getDate();      //返回日期月份中的天数（1到31）
      var hour = now.getHours();     //返回日期中的小时数（0到23）
      var minute = now.getMinutes(); //返回日期中的分钟数（0到59）
      var second = now.getSeconds(); //返回日期中的秒数（0到59）
      return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  },
  //文章点赞
  zanClick(){
    let that=this;
    a.post("sns/board/like", {
      bid:that.data.info.board.id,
      pid:that.data.id
    }, function(res) {
      if(that.data.info.isgood==0){//未点赞
        let goodcount=that.data.info.goodcount;
        goodcount++;
        that.setData({
          'info.isgood':1,
          'info.goodcount':goodcount,
        })
      }else{
        let goodcount=that.data.info.goodcount;
        if(goodcount>0){
          goodcount--;
        }
        that.setData({
          'info.isgood':0,
          'info.goodcount':goodcount,
        })
      }
    })
  },
  //回复评论点赞
  likeSecond(e){
    let that=this;
    let index=e.currentTarget.dataset.index;
    let isgood='dataList['+index+'].isgood';
    let goodcount='dataList['+index+'].goodcount';
    a.post("sns/board/like", {
      bid:that.data.info.board.id,
      pid:that.data.dataList[index].id
    }, function(res) {
      if(that.data.dataList[index].isgood==0){//未点赞
        let oldgoodcount=that.data.dataList[index].goodcount;
        oldgoodcount++;
        that.setData({
          [isgood]:1,
          [goodcount]:oldgoodcount,
        })
      }else{
        let oldgoodcount=that.data.dataList[index].goodcount;
        if(oldgoodcount>0){
          oldgoodcount--;
        }
        that.setData({
          [isgood]:0,
          [goodcount]:oldgoodcount,
        })
      }
    })
  },
  goComment(){
    wx.navigateTo({
      url: '/pages/community/article/comment/index?bid='+this.data.info.board.id+'&pid='+this.data.id,
    })
  },
  //回复评论
  commentSecond(e){
    wx.navigateTo({
      url: '/pages/community/article/comment/index?bid='+this.data.info.board.id+'&pid='+this.data.id+'&rpid='+e.currentTarget.dataset.rpid,
    })
  },
  //查看他人主页
  goPerson(){
    wx.navigateTo({
      url: '/pages/community/otherHome/index?openid='+this.data.info.member.id,
    })
  },
  //点击评论查看他人主页
  goPersonComment(e){
    wx.navigateTo({
      url: '/pages/community/otherHome/index?openid='+e.currentTarget.dataset.openid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.setData({
      pageIndex:1
    })
    this.geAticle();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {
    let index = this.data.pageIndex;
    index++;
    if (index > this.data.totalPage) {
      this.setData({
        pageIndex: this.data.totalPage
      })
      return
    } else {
      this.setData({
        pageIndex: index
      })
    }
    this.getcommnt();
  },
})